//
//  RootViewController.m
//  TelethonMobile
//
//  Created by Cyril ALIX on 03/12/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "RootViewController.h"
#import "TMServerApi.h"
#import "Lieu.h"
#import "MapController.h"

static TMServerApi* tmsInstance;

@implementation RootViewController

@synthesize locationArray;
@synthesize myLocation;

- (void)viewDidLoad
{
    [super viewDidLoad];

    tmsInstance = [TMServerApi sharedInstance];
    [tmsInstance fetchLocationsWithDistance:5.0f andLongitude:2.3721549f andLatitude:48.8531475f andDelegate:self];
    
//    UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(insertNewObject)];
//    self.navigationItem.rightBarButtonItem = addButton;
//    [addButton release];
    
    self.title = @"TéléthonMobile";
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
//    
//    TMServerApi* tmJsonInstance = [TMServerApi sharedInstance];
//    
//    Lieu* loin = [[Lieu alloc]initWithNom:@"Tresloin" andLongitude:78.765543f andLatitude:124.212121f andCategorie:9];
//
//    [tmJsonInstance insertLocation:loin];
//    
//    
//    NSArray* dictionaryArray = [tmJsonInstance fetchLocationsFromJsonWithDistance:3.432f andLongitude:2.3721549f andLatitude:48.8531475f];
//    
//    NSArray* lieuArray = [Lieu lieuArrayFromNSDictionary:dictionaryArray];
//    
//    for (Lieu* leLieu in lieuArray) {
//        NSLog(@"%@", [leLieu description]);
//    };
}

- (void)viewWillDisappear:(BOOL)animated
{
	[super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
	[super viewDidDisappear:animated];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Relinquish ownership any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload
{
    [super viewDidUnload];

    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
    // For example: self.myOutlet = nil;
}

- (void)dealloc
{
    self.locationArray = nil;
    [super dealloc];
}

- (IBAction) buttonTouched:(UIButton *) sender
{
    //Ici on filtre les location suivant ce qu'on a cliqué...
    int filter = sender.tag;
    NSMutableArray* locationFiltered = [[NSMutableArray alloc]init];
    
    if (filter != 0) {
        for (Lieu* leLieu in self.locationArray) {
            if ([leLieu _categorie] == filter) {
                [locationFiltered addObject:leLieu];
            }
        }
    } else {
        locationFiltered = [NSMutableArray arrayWithArray:self.locationArray];
    }
    
    MapController *mapController = [[MapController alloc] initWithLocationArray:locationFiltered];
    [self.navigationController pushViewController:mapController animated:YES];
    [mapController release];
}

#pragma mark -
#pragma mark delegate

- (void) finishWithData:(NSArray *)theArray {
    self.locationArray = theArray;
}


@end
